from flask import jsonify


class MessageResponse(object):

    def __init__(self, code, data=None):
        """
        用户数据返回
        :param status:
            200: 请求成功
            401: 认证错误
            400: 请求参数错误
            500: 服务器内部错误
        :param data:
            {
                field1: message,
                field2: message,
            }
        """
        self.code = code
        self.data = data or {}


def success(data=None):
    msg_resp = MessageResponse(code=200, data=data)
    return jsonify(msg_resp.__dict__)


def auth_error(data):
    msg_resp = MessageResponse(code=401, data=data)
    return jsonify(msg_resp.__dict__)


def params_error(data):
    msg_resp = MessageResponse(code=400, data=data)
    return jsonify(msg_resp.__dict__)


def server_error(data):
    msg_resp = MessageResponse(code=500, data=data)
    return jsonify(msg_resp.__dict__)


def csrf_error(data):
    msg_resp = MessageResponse(code=400, data=data)
    return jsonify(msg_resp.__dict__)


def permissions_error(data):
    msg_resp = MessageResponse(code=403, data=data)
    return jsonify(msg_resp.__dict__)
